﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommunitySystemModel;
using System.Collections.ObjectModel;
using System.Data;

namespace CommunitySystem.BO
{
   public static class SysInfoBO
    {
       public static ObservableCollection<SystemInfo> GetSystemInfoByConn(string conn,int start, int pagesize)
       {
           ObservableCollection<SystemInfo> syss = new ObservableCollection<SystemInfo>();
           string sql = string.Format( @"select top({1})* from 
                                        (
                                        select ROW_NUMBER() over (order by CreateDate desc) rowID,* from dbo.SystemInfo
                                        )a where 1=1 {0}
                                        and a.rowID>=
                                        (
                                        select ISNULL(MAX(c.rowID),0) from
                                        (
                                        select top({1}*({2}-1)+1)* from
                                        (
	                                        select ROW_NUMBER() over (order by CreateDate desc) rowID,* from dbo.SystemInfo where 1=1 {0}
                                        )b
                                        )c
                                        )",conn,pagesize,start);
           DataSet ds = AccessHelper.GetInstance().ExcuteDataSet(sql);
           foreach (DataRow dr in ds.Tables[0].Rows)
           {
               syss.Add(SystemInfo.FillEntity(dr));
           }
           return syss;
       }

       public static int GetSystemInfoByCount(string conn)
       {
           string sql = string.Format("select count(*) from dbo.SystemInfo where 1=1 {0}", conn);
           DataSet ds = AccessHelper.GetInstance().ExcuteDataSet(sql);
           return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
       }
       public static void UpdateSystemInfoByCount(string conn)
       {
           string sql = string.Format("update dbo.SystemInfo set status=1 ,ChaKanSJ=GETDATE() where 1=1 {0}", conn);
          AccessHelper.GetInstance().ExecuteNonQuery(sql);
         
       }
       public static void AddSystemInfo(SystemInfo sysinfo)
       {
           string sql = string.Format(@"INSERT INTO SystemInfo
           ([MenuId],[MoKuai],[MoKuaiSX],[DW],[CreateUser],[CreateDate],[CreateUserID],OrgIDs,LogUID)
     VALUES
           ({0},'{1}','{2}','{3}','{4}','{5}','{6}','{7}',{8})", sysinfo.MenuId, sysinfo.MoKuai, sysinfo.MoKuaiSX, sysinfo.DW1, sysinfo.CreateUser, sysinfo.CreateDate, sysinfo.CreateUserID, sysinfo.OrgIDs, sysinfo.LogUID);
           AccessHelper.GetInstance().ExecuteNonQuery(sql);

       }
    }
}
